package com.example.myapplication_4

import android.content.Intent
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity

class EditProfileActivity : AppCompatActivity() {

    private lateinit var editAvatarImageView: ImageView
    private lateinit var editNameEditText: EditText
    private lateinit var editStudentIdEditText: EditText
    private lateinit var editPhoneEditText: EditText
    private lateinit var editEmailEditText: EditText
    private lateinit var editGenderRadioGroup: RadioGroup
    private lateinit var travelCheckBox: CheckBox
    private lateinit var readingCheckBox: CheckBox
    private lateinit var gamingCheckBox: CheckBox
    private lateinit var shoppingCheckBox: CheckBox
    private lateinit var saveEditButton: Button
    private lateinit var cancelButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_profile)

        initViews()
        setupClickListeners()
        loadSavedUserInfo()
    }

    private fun initViews() {
        editAvatarImageView = findViewById(R.id.editAvatarImageView)
        editNameEditText = findViewById(R.id.editNameEditText)
        editStudentIdEditText = findViewById(R.id.editStudentIdEditText)
        editPhoneEditText = findViewById(R.id.editPhoneEditText)
        editEmailEditText = findViewById(R.id.editEmailEditText)
        editGenderRadioGroup = findViewById(R.id.editGenderRadioGroup)
        travelCheckBox = findViewById(R.id.travelCheckBox)
        readingCheckBox = findViewById(R.id.readingCheckBox)
        gamingCheckBox = findViewById(R.id.gamingCheckBox)
        shoppingCheckBox = findViewById(R.id.shoppingCheckBox)
        saveEditButton = findViewById(R.id.saveEditButton)
        cancelButton = findViewById(R.id.cancelButton)
    }

    private fun setupClickListeners() {
        saveEditButton.setOnClickListener {
            saveEditedInfo()
        }

        cancelButton.setOnClickListener {
            finish() // 返回登录界面
        }

        editAvatarImageView.setOnClickListener {
            Toast.makeText(this, "更换头像", Toast.LENGTH_SHORT).show()
        }
    }

    private fun loadSavedUserInfo() {
        val sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE)

        // 从SharedPreferences加载已保存的数据
        editNameEditText.setText(sharedPreferences.getString("name", ""))
        editStudentIdEditText.setText(sharedPreferences.getString("studentId", ""))
        editPhoneEditText.setText(sharedPreferences.getString("phone", ""))
        editEmailEditText.setText(sharedPreferences.getString("email", ""))

        // 加载性别
        val gender = sharedPreferences.getString("gender", "")
        if (gender == "男") {
            editGenderRadioGroup.check(R.id.editMaleRadioButton)
        } else if (gender == "女") {
            editGenderRadioGroup.check(R.id.editFemaleRadioButton)
        }

        // 加载爱好
        val hobbies = sharedPreferences.getStringSet("hobbies", setOf())
        if (hobbies != null) {
            travelCheckBox.isChecked = hobbies.contains("旅游")
            readingCheckBox.isChecked = hobbies.contains("阅读")
            gamingCheckBox.isChecked = hobbies.contains("游戏")
            shoppingCheckBox.isChecked = hobbies.contains("购物")
        }

        // 修正：使用安全调用和空值检查
        val savedName = sharedPreferences.getString("name", "")
        if (savedName.isNullOrEmpty()) {
            editNameEditText.hint = "请输入姓名"
            editStudentIdEditText.hint = "请输入学号"
            editPhoneEditText.hint = "请输入电话号码"
            editEmailEditText.hint = "请输入电子邮件地址"
        }

    }

    private fun saveEditedInfo() {
        val name = editNameEditText.text.toString()
        val studentId = editStudentIdEditText.text.toString()
        val phone = editPhoneEditText.text.toString()
        val email = editEmailEditText.text.toString()

        // 验证必填字段
        if (name.isBlank() || studentId.isBlank()) {
            Toast.makeText(this, "请填写姓名和学号", Toast.LENGTH_SHORT).show()
            return
        }

        // 获取性别
        val gender = when (editGenderRadioGroup.checkedRadioButtonId) {
            R.id.editMaleRadioButton -> "男"
            R.id.editFemaleRadioButton -> "女"
            else -> ""
        }

        // 获取爱好
        val hobbies = mutableListOf<String>()
        if (travelCheckBox.isChecked) hobbies.add("旅游")
        if (readingCheckBox.isChecked) hobbies.add("阅读")
        if (gamingCheckBox.isChecked) hobbies.add("游戏")
        if (shoppingCheckBox.isChecked) hobbies.add("购物")

        // 保存到 SharedPreferences
        saveToPreferences(name, studentId, phone, email, gender, hobbies)

        Toast.makeText(this, "个人信息修改成功", Toast.LENGTH_SHORT).show()

        // 设置结果，通知LoginActivity更新显示
        setResult(RESULT_OK)
        finish() // 返回登录界面
    }

    private fun saveToPreferences(name: String, studentId: String, phone: String,
                                  email: String, gender: String, hobbies: List<String>) {
        val sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE)
        val editor = sharedPreferences.edit()

        editor.putString("name", name)
        editor.putString("studentId", studentId)
        editor.putString("phone", phone)
        editor.putString("email", email)
        editor.putString("gender", gender)
        editor.putStringSet("hobbies", hobbies.toSet())

        editor.apply()
    }
}